﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;


namespace KPIS.GERP.WEB.GIMS.Controllers
{
   
    public class DeliveryNotePenaltyController : Controller
    {
        
        public ActionResult Index(int DnId,int? PoId)
        {
            var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
            var service2 = new DeliveryNoteService.DeliveryNoteServiceClient();
            var DNPen = service.SelectDeliveryNotePenalty(new Models.ICM.DeliveryNotePenalty {DnId=DnId}, new[] { "PurchaseOrderPenalty", "PurchaseOrderPenalty.PurchaseOrder", "DeliveryNote" });//.Where(x => x.DnPenId > 0).ToList());

            var service1 = new DeliveryNoteService.DeliveryNoteServiceClient();
            var modelDn = service1.SelectDeliveryNote(new Models.ICM.DeliveryNote { DnId = DnId }, null).FirstOrDefault();
            var DNPenId = service.SelectDeliveryNotePenalty(new Models.ICM.DeliveryNotePenalty { DnId = DnId },null).FirstOrDefault();

            ViewBag.DnId = DnId;
            ViewBag.PoId = PoId ?? 0;

            string RecordStatus = modelDn.RecordStatus;
            ViewBag.bEditable = RecordStatus != Models.SYS.RecordStatus.ApprovedStatus && RecordStatus != Models.SYS.RecordStatus.ProcessStatus;

            return View(DNPen);
        }

        public ActionResult Create(int? DnId,int? PoId)
        {
            ViewBag.DnId = DnId ?? 0;
            ViewBag.PoId = PoId ?? 0;
            DeliveryNotePenalty DnPoPen = new DeliveryNotePenalty();
            var service = new DeliveryNoteService.DeliveryNoteServiceClient();
            var serviceItem = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();

            var DN = service.SelectDeliveryNote(new Models.ICM.DeliveryNote { DnId = DnId ?? 0 }, new[] { "PurchaseOrder" });//.Where(x => x.DnPenId > 0).ToList());
            var Po = service.SelectPurchaseOrder(new Models.ICM.PurchaseOrder { PoId = PoId ?? 0 }, null);

            var Popen = serviceItem.SelectPurchaseOrderPenalty(new Models.ICM.PurchaseOrderPenalty { PoId = PoId }, new[] { "PurchaseOrder" });

            var oData = Popen.Where(t => t.PoId == PoId).FirstOrDefault();
            var OModel = DN.Where(t => t.DnId == DnId).FirstOrDefault();
                    
            if(OModel != null)
            {
                      DateTime? d1 = OModel.PurchaseOrder.DeliveryDate; //ปัจจุบัน

                      DateTime? d2 = OModel.ReceiveDate; 
                      TimeSpan? a = d1 - d2;
                      Double Day = Math.Abs(a.Value.Days);
                      
                if ((oData != null))
                {
                    DnPoPen.DnId = DnId;
                    DnPoPen.PoPenId = oData.PoPenId;
                    DnPoPen.PenaltyAmount = oData.PenaltyRate;

                    //DnPoPen.PenaltyCalculate = 1;//จำนวนวัน

                    //int? Calculate = Convert.ToInt32(oData.PenaltyRate * Convert.ToDouble(a.Value.Days));//เอาค่าปรับมาคูนจำนวนวัน
                    int? Calculate = Convert.ToInt32(oData.PenaltyRate * Day);//เอาค่าปรับมาคูนจำนวนวัน
                    DnPoPen.PenaltyCalculate = Calculate;
                    ViewData["Calculate"] = Calculate;
                    ViewData["PenaltyName"] = oData.Name;
                    ViewData["Descr"] = oData.Descr;
                    
                }
            }
            return View(DnPoPen);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DeliveryNotePenalty model, int? DnId, string txtPenaltyCalculate)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.PenaltyCalculate = Convert.ToInt32(txtPenaltyCalculate);
                    var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                    var id = service.InsertDeliveryNotePenalty(model, Class.GimsMembership.CurrentEmpId);
                    return RedirectToAction("Index", new { DnId });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return View(model);
        }

        public ActionResult Edit(int DnPenId)
        {
            var serviceItem = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
            var DnPoPen = serviceItem.SelectDeliveryNotePenalty(new Models.ICM.DeliveryNotePenalty { DnPenId = DnPenId }, new[] { "PurchaseOrderPenalty" }).FirstOrDefault();

            ViewData["Calculate"] = DnPoPen.PenaltyCalculate;
            ViewData["PenaltyName"] = DnPoPen.PurchaseOrderPenalty.Name;
            ViewData["Descr"] = DnPoPen.PurchaseOrderPenalty.Descr;
            ViewData["FinalAmount"] = DnPoPen.PenaltyAmount;

            return View(DnPoPen);

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(DeliveryNotePenalty model, int? DnId, string txtPenaltyCalculate)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.PenaltyCalculate = Convert.ToInt32(txtPenaltyCalculate);
                    var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                    service.UpdateDeliveryNotePenalty(model, Class.GimsMembership.CurrentEmpId);
                    return RedirectToAction("Index", new { DnId });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return View(model);
        }

        public ActionResult Delete(int DnPenId, int DnId)
        {
            try
            {
                var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                service.DeleteDeliveryNotePenalty(DnPenId, Class.GimsMembership.CurrentEmpId);
                return RedirectToAction("Index", new { DnId });
            }
            catch
            {
                return RedirectToAction("Index", new { DnId });
            }
        }

        public ActionResult Details(int DnPenId)
        {
            var serviceItem = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
            var DnPoPen = serviceItem.SelectDeliveryNotePenalty(new Models.ICM.DeliveryNotePenalty { DnPenId = DnPenId }, new[] { "PurchaseOrderPenalty" }).FirstOrDefault();

            return View(DnPoPen);
        }


    }
}
 